﻿/**
 * 
 */
package complex.cs;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.PopupMenu;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

/**
 * @author zhenghx
 * C/S结构聊天室 - 客户端程序
 */
public class MyClient extends Frame implements ActionListener, ItemListener {

    MenuBar m_Menu_Bar;
    Menu menuFile, menuEdit, menuHelp;
    MenuItem mi_File_Open, mi_File_Close, mi_File_Exit, mi_Edit_Copy, mi_Edit_Paste;
    MenuItem pi_New, pi_Del, pi_Pro, mi_Help_Sub;
    CheckboxMenuItem mi_Edit_Cut;
    PopupMenu popM;
    Socket ClientSocket;
    PrintStream os;
    DataInputStream is;
    String s;
    Label MyLabel = new Label("==欢迎使用本系统为您提供服务==");
    TextArea textarea;
    Button MyButton = new Button("发 送 信 息");
    
    public MyClient() {
        setTitle("Client Window(客户端窗口)");
        setLayout(new BorderLayout());
        this.addWindowListener(new WinAdptClient(this));    // 向窗体添加监听器
        MyButton.addActionListener(this);   // 为发送按钮设置添加事件监听器
        textarea = new TextArea(13, 55);    // 界面设计，添加下拉菜单和弹出菜单等
        
        popM = new PopupMenu();
        pi_New = new MenuItem("新建");
        pi_New.addActionListener(this);
        popM.add(pi_New);
        pi_Del = new MenuItem("删除");
        pi_Del.addActionListener(this);
        popM.add(pi_Del);
        pi_Pro = new MenuItem("属性");
        pi_Pro.addActionListener(this);
        popM.add(pi_Pro);
        
        m_Menu_Bar = new MenuBar();
        menuFile = new Menu("文件");
        mi_File_Open = new MenuItem("打开");
        mi_File_Open.setShortcut(new MenuShortcut('f'));
        mi_File_Close = new MenuItem("关闭", new MenuShortcut('s'));
        mi_File_Exit = new MenuItem("退出", new MenuShortcut('x'));
        mi_File_Open.setActionCommand("打开");
        mi_File_Close.setActionCommand("关闭");
        mi_File_Exit.setActionCommand("退出");
        mi_File_Open.addActionListener(this);
        mi_File_Close.addActionListener(this);
        mi_File_Exit.addActionListener(this);
        menuFile.add(mi_File_Open);
        menuFile.add(mi_File_Close);
        menuFile.add(mi_File_Exit);
        m_Menu_Bar.add(menuFile);
        
        menuEdit = new Menu("编辑");
        mi_Edit_Copy = new MenuItem("复制");
        mi_Edit_Paste = new MenuItem("粘贴");
        mi_Edit_Cut = new CheckboxMenuItem("剪切");
        mi_Edit_Copy.setActionCommand("复制");
        mi_Edit_Paste.setActionCommand("粘贴");
        mi_Edit_Copy.addActionListener(this);
        mi_Edit_Paste.addActionListener(this);
        mi_Edit_Cut.addActionListener(this);
        menuEdit.add(mi_Edit_Copy);
        menuEdit.add(mi_Edit_Paste);
        menuEdit.addSeparator();
        menuEdit.add(mi_Edit_Cut);
        m_Menu_Bar.add(menuEdit);
        
        menuHelp = new Menu("帮助");
        mi_Help_Sub = new MenuItem("主题");
        menuHelp.add(mi_Help_Sub);
        m_Menu_Bar.add(menuHelp);
        this.setMenuBar(m_Menu_Bar);
        add("North", MyLabel);
        add("South", MyButton);
        add("Center", textarea);
        setResizable(false);    // 设置窗体大小不可改变
        pack();
        show();     // 显示窗体
        connect();  // 连接服务器端
    }
    
    /**
     * 与服务器端连接程序
     */
    public void connect() {
        try {
            ClientSocket = new Socket("127.0.0.1", 6544);    // 服务器应用程序端口设为6544
            os = new PrintStream(   // 建立输入输出流对象
                    new BufferedOutputStream(ClientSocket.getOutputStream()));
            is = new DataInputStream(
                    new BufferedInputStream(ClientSocket.getInputStream()));
            s = is.readLine();
            textarea.appendText(s + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        new MyClient(); // 建立一个客户端对象
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "退出")   {   // 事件响应
            dispose();
            System.exit(0);
        }
        if (e.getSource() == MyButton) {    // 单击“发送”按钮向服务器发送消息
            try {
                textarea.setEnabled(false);
                textarea.append("\n");
                os.print(textarea.getText());
                os.flush();
                textarea.setEnabled(true);
                textarea.setText("");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 响应CHECKBOXMENUITEM被单击事件
     */
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == mi_Edit_Cut) {
            if (((CheckboxMenuItem)e.getSource()).getState()) {
                textarea.setText("\n\n\n\n\n\n\t\t\t" + "you have chosen " +
                        ((CheckboxMenuItem)e.getSource()).getLabel());
            } else {
                textarea.setText("\n\n\n\n\n\n\t\t\t" + "you have not chosen " +
                        ((CheckboxMenuItem)e.getSource()).getLabel());
            }
        }
    }
    
    class WinAdptClient extends WindowAdapter {
        MyClient m_Parent;
        
        public WinAdptClient(MyClient p) {
            m_Parent = p;
        }
        
        public void windowClosing(WindowEvent e) {
            // 关闭窗口前向SERVER端发送结束消息，并关闭各输入输出流与连接
            try {
                m_Parent.os.println("Bye");
                m_Parent.os.flush();
                m_Parent.is.close();
                m_Parent.os.close();
                m_Parent.ClientSocket.close();
                m_Parent.dispose();
                System.exit(0);
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

}
